#!/bin/sh
#-*-sh-*-

#
# Copyright © 2009 CNRS
# Copyright © 2009-2015 Inria.  All rights reserved.
# Copyright © 2009, 2011 Université Bordeaux
# See COPYING in top-level directory.
#

HWLOC_top_builddir="@HWLOC_top_builddir@"
calc="$HWLOC_top_builddir/utils/hwloc/hwloc-calc"
HWLOC_top_srcdir="@HWLOC_top_srcdir@"

HWLOC_PLUGINS_PATH=${HWLOC_top_builddir}/src
export HWLOC_PLUGINS_PATH

: ${TMPDIR=/tmp}
{
  tmp=`
    (umask 077 && mktemp -d "$TMPDIR/fooXXXXXX") 2>/dev/null
  ` &&
  test -n "$tmp" && test -d "$tmp"
} || {
  tmp=$TMPDIR/foo$$-$RANDOM
  (umask 077 && mkdir "$tmp")
} || exit $?
file="$tmp/test-hwloc-calc.output"

set -e
(
  $calc --if synthetic --input "node:4 core:4 pu:4" root
  $calc --if synthetic --input "node:4 core:4 pu:4" all --taskset
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" 0xf
  $calc --if synthetic --input "node:4 core:4 pu:4" 0xf ~0x3 0xff0 '^0xf0'
  $calc --if synthetic --input "node:4 core:4 pu:4" core:0 pu:15 ~pu:0 '^pu:2'
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:even
  $calc --if synthetic --input "node:4 core:4 pu:4" node:2-
  $calc --if synthetic --input "node:4 core:4 pu:4" core:12-
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:62:10
  $calc --if synthetic --input "node:4 core:4 pu:4" core:all.pu:1:2
  $calc --if synthetic --input "node:4 core:4 pu:4" core:odd.pu:0
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:6:2 core:3-4.pu:1-3 node:2.pu:14:2 node:3.core:3.pu:3
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" root --number-of node
  $calc --if synthetic --input "node:4 core:4 pu:4" node:2 -N core
  $calc --if synthetic --input "node:4 core:4 pu:4" node:2 -N 3
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" root --intersect Machine
  $calc --if synthetic --input "node:4 core:4 pu:4" core:4-7 -I NUMANode
  $calc --if synthetic --input "node:4 core:4 pu:4" core:10-15 -I 1
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:2-3 --hierarchical node.pu
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:3-6 -H node.core --sep foo
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:3-6 -H core.pu
  $calc --if synthetic --input "node:4 core:4 pu:4" -I pu `$calc --if synthetic --input "node:4 core:4 pu:4" pu:3-6 -H core.pu`
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:11:4 -H node.core.pu
  $calc --if synthetic --input "node:4 core:4 pu:4" -I pu `$calc --if synthetic --input "node:4 core:4 pu:4" pu:11:4 -H node.core.pu`
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:12-37 --largest
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:22-47 --largest --sep "_"
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:22-47 --single
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:22-47 --single --pulist
  echo
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:33-37 --pulist --po
  $calc --if synthetic --input "node:4 core:4 pu:4" pu:30-37 --nodelist --po --sep foo
  echo
  $calc --if synthetic --input "group:2 cache:2 pu:1" group:0 cache:3
  $calc --if synthetic --input "group:2 group:2 cache:2 cache:2 pu:1" Group2:1 Group1:0
  $calc --if synthetic --input "group:2 group:2 cache:2 cache:2 pu:1" l2:0-2 L1cache:13:3
  echo
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml os=eth6 os=eth4
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml os=sdc os=sr0 ~os=sda
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml os:7-8
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci:10-11
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci'[1000:0062]':3:2
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci'[14e4:1639]':1
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci'[14e4:1639]':2-5
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci=0000:62:00.1 pci=0000:02:00.1
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci=0000:62:00.1 xpci=0000:02:00.0
  $calc --if xml --input $HWLOC_top_srcdir/tests/xml/96em64t-4n4d3ca2co-pci.xml pci=0000:02:00.0 xpci=0000:02:00.1
  echo
  cat << EOF | $calc --if synthetic --input "node:4 core:4 pu:4"
pu:0
core:0 core:15
node:0 node:3
0x0001 0x00002 0x000004 0x000000000008
0x0000000000000000000000000000000000000000000000000000000000000000000000000000001
0x1,0x0,0x0
root
EOF
) > "$file"
diff @HWLOC_DIFF_U@ $HWLOC_top_srcdir/utils/hwloc/test-hwloc-calc.output "$file"
rm -rf "$tmp"
